package com.fxsh168.server.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.entitys.MyMsgQREntity;

import java.util.List;


/**
 * 自定义消息adapter类
 */
public class MsgAdapter extends BaseAdapter {
    private List<MyMsgQREntity.DataBean>listData;//数据源
    private Context context;//上下文

    public MsgAdapter(List<MyMsgQREntity.DataBean>listData, Context context) {
        this.listData = listData;
        this.context = context;
    }

    public MsgAdapter() {
    }

    @Override
    public int getCount() {
        return listData==null?0:listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.msg_item,null);
            viewHolder=new ViewHolder();
            viewHolder.msgportrait= (ImageView) convertView.findViewById(R.id.msgportrait);//复用头像
            viewHolder.msg= (TextView) convertView.findViewById(R.id.msg);//复用消息
            viewHolder.msgtime= (TextView) convertView.findViewById(R.id.msgtime);//复用时间
            viewHolder.msgDetails= (TextView) convertView.findViewById(R.id.msgDetails);//复用消息详情

            convertView.setTag(viewHolder);//给convertView设置标签
        }else {
            viewHolder= (ViewHolder) convertView.getTag();//通过标签获得viewHolder
        }

        Log.e("listData","listData="+listData.size());
        //获得数据
        MyMsgQREntity.DataBean dataBean = listData.get(position);
//        String photo = dataBean.getPhoto().toString().trim();
//        String msg = dataBean.getContent().toString().trim();
        String msgtime = dataBean.getPropellingtime().toString().trim();
        String msgDetails = dataBean.getContent().toString().trim();
        //获得头像Bitmap并设置到控件上
//        Picasso.with(context).load(photo).centerCrop().into(viewHolder.msgportrait);
//        viewHolder.msg.setText(msg);
        viewHolder.msgtime.setText(msgtime);
        viewHolder.msgDetails.setText(msgDetails);

        return convertView;
    }


    //创建ViewHolder内部类
    class ViewHolder{
       ImageView msgportrait;
       TextView msg,msgtime,msgDetails;
    }
}
